package com.ht.log;

import com.ht.aems.util.ObjectBuffer;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Dong Shufeng
 * Date: 11-10-18
 */
public class RtAlarmUtil {
    private static Logger log = Logger.getLogger(RtAlarmUtil.class);

    private static Map<String, RtAlarmCreator> alarmCreators = new HashMap<String, RtAlarmCreator>();
    private static boolean isRunning = false;
    private static boolean isStoped = false;

    public static void startAlarmThread() {
        if(isRunning)
            return;
        isRunning = true;
        Thread thread = new Thread() {
            public void run() {
                while (!isStoped) {
                    try {
                        for(RtAlarmCreator creator : alarmCreators.values())
                            creator.createAlarm();
                        sleep(30000);
                    } catch (Exception e) {
                        log.warn("Exception when creating real time alarm: " + e);
                    }
                }
            }
        };
        thread.start();
    }

    public static void registor(String name, RtAlarmCreator creator) {
        alarmCreators.put(name,  creator);
    }

    public static boolean isStoped() {
        return isStoped;
    }

    public static void setStoped(boolean stoped) {
        isStoped = stoped;
    }
}
